package com.lyc.demo.controller;

import com.lyc.demo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@RestController
@RefreshScope
public class HelloController {

    @Autowired
    HelloService helloService;

    @Value("${from}")
    private String from;

    @RequestMapping("/hello")
    public String home(String name, ModelAndView modelAndView) {
        String result = helloService.home(name);
//        return "hi " + name + " ,i am from port:" + port;
        modelAndView.addObject("result", result);
        return "接收到调用结果：" + result + " " + from;
    }

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/testRibbon")
    public String testRibbon(@RequestParam(value = "name", defaultValue = "kkk") String name) {
        Map<String, String> param = new HashMap<>();
        param.put("name", name);
        String data = restTemplate.postForObject("http://service-hi/hi2", param, String.class);
        //String data = restTpl.getForObject("http://127.0.0.1:8081/demo/data", String.class);
        return "接收到调用结果：" + data + " " + from;
    }

}
